let $ = jQuery;
let reqUrl;
$(function () {
    //根据窗口调整表格高度
    $(window).resize(function() {
        $('#exampleTableEvents').bootstrapTable('resetView', {
            height: tableHeight()
        })
    })

    $('#exampleTableEvents').bootstrapTable({
        method:"post",
        url: "/receipts/pageSaleStateMent",
        contentType: "application/x-www-form-urlencoded",
        dataType:"json",
        height:tableHeight(),//高度调整
        striped: true, //是否显示行间隔色
        pageNumber: 1, //初始化加载第一页，默认第一页
        pagination: true,//是否分页
        onLoadSuccess:onLoadSuccess,
        sidePagination: 'server',//在服务器分页
        queryParamsType:'limit',
        queryParams:queryParams,
        pageSize: 20,//单页记录数
        showRefresh: true,//刷新按钮
        clickToSelect: true,//是否启用点击选中行
        showToggle: true,
        showColumns: true,
        iconSize: 'outline',
        toolbar: '#exampleTableEventsToolbar',
        icons: {
            refresh: 'glyphicon-repeat',
            toggle: 'glyphicon-list-alt',
            columns: 'glyphicon-list'
        },
        columns: [
            {
                title: 'ID',
                field: 'oid',
                align: 'center',
                visible: false
            },
            {
                title: '订单编号',
                field: 'ono',
                align: 'center',
                sortable: false
            },
            {
                title: '成交时间',
                field: 'createTime',
                formatter:function (value, row, index){
                    return  formatDate(row.createTime)
                },
                align: 'center',
                sortable: false
            },
            {
                title: '售卖价格(元)',
                align: 'center',
                field: 'salePrice',
                formatter:function (value, row, index){
                    let sum =0.00;
                    for (let i = 0; i <row.saleStateMentInfo.length ; i++) {
                         sum=sum+(row.saleStateMentInfo[i].salePrice)*(row.saleStateMentInfo[i].number);
                    }
                    return sum;
                },
            },
            {
                title: '进货价格(元)',
                align: 'center',
                field: 'productPrice',
                formatter:function (value, row, index){
                    let sum =0.00;
                    for (let i = 0; i <row.saleStateMentInfo.length ; i++) {
                        sum=sum+(row.saleStateMentInfo[i].productPrice)*(row.saleStateMentInfo[i].number);
                    }
                    return sum;
                },
            },
            {
                title: '商品利润(元)',
                align: 'center',
                field: 'profitPrice',
                formatter:profitPrice
            },
            {
                title: '操作人',
                align: 'center',
                field: 'operator',
            },
            {
                title: '操作',
                field: 'Button',
                align: 'center',
                events: operateEvents,//事件
                formatter: AddFunctionAlty//添加按钮
            }
        ],
        locale: 'zh-CN',//中文支持,
    });
    //点击搜索
    $('#btn-search').click(function () {
        $('#exampleTableEvents').bootstrapTable('refresh', {url: '/receipts/pageSaleStateMent'});//url为后台action
    })
})

function tableHeight() {
    return $(window).height() - 150;
}

//列表行‘操作’按钮
function AddFunctionAlty(value, row, index) {
    return '<button id="edit" type="button" class="btn btn-default">详情</button>'

}
//请求服务数据时所传查询参数
function queryParams(params){
    return{
        pageSize: params.limit,
        pageNum:parseInt(params.offset/params.limit)+1,
        typeid:$("#typeid_search").val(),
        status:$('#status_search').val(),
        startTime:$('#startTime_search').val(),
        endTime:$('#endTime_search').val()
    }
}
//点击详情按钮事件
window.operateEvents = {
    "click #edit": function (e, value, row, index) {
        $("#title").html("单据详情");
        $("#attachment").html("");
        $("#createTime").html(formatDate(row.createTime));
        $("#ono").html(row.ono);
        $("#operator").html(row.operator);
        //查询单据详情
        $.ajax("receipts/findSaleStateMentByOid",{
            method:"post",
            headers:{
                contentType: "application/x-www-form-urlencoded",
            },
            data:{oid:row.oid},
            success:(res)=>{
                //数据渲染
                let data = res.rows[0].saleStateMentInfo;
                let total =0.00;
                $("#data").html("");
                for (let dataKey of data) {
                    total =dataKey.totalPrice;
                    let el = "<tr>" +
                            "<td>"+dataKey.name+"</td>" +
                            "<td>"+dataKey.number+"</td>" +
                            "<td>"+dataKey.productPrice+"</td>" +
                            "<td>"+dataKey.salePrice+"</td>" +
                            "<td>"+dataKey.profitPrice+"</td>" +
                            "</tr>";
                    $("#data").append(el);
                }
                $("#salePrice").html(total);
                $("#profitPrice").html(profitPrice(value,row));
            }
        })
        $("#myModal5").modal("show");
    },
}
function profitPrice(value, row){
    //计算单笔订单利润
    let sum =0.00;
    for (let i = 0; i <row.saleStateMentInfo.length ; i++) {
        sum=sum+(row.saleStateMentInfo[i].profitPrice)*(row.saleStateMentInfo[i].number);
    }
    return sum;
}
function onLoadSuccess(res){
    //订单交易成功笔数
    $("#totalSize").html("订单总成交数:"+res.total+"笔")

    let allPrice = 0.00;
    let allProfitPrice = 0.00;
    let rowList = res.rows;
    for (let rowsKey in rowList) {
        //订单交易成功总金额
        allPrice=allPrice+rowList[rowsKey].saleStateMentInfo[0].totalPrice;
        let stateMentInfo = rowList[rowsKey].saleStateMentInfo;
        //订单交易成功总利润
        let thisPofitPrice = 0.00;
        for (let i = 0; i < stateMentInfo.length; i++) {
            thisPofitPrice = thisPofitPrice+ stateMentInfo[i].profitPrice*stateMentInfo[i].number;
        }
        allProfitPrice=allProfitPrice+thisPofitPrice;
    }
    $("#allTotalPrice").html("订单总成交额:"+allPrice+"元")
    $("#allProfitPrice").html("订单总成交利润:"+allProfitPrice+"元")
}
